package com.chainyoung.component_tmperson.ui.activity.debit;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.di.component.DaggerPersonActivityComponent;
import com.chainyoung.component_tmperson.di.module.PersonActivityModule;
import com.chainyoung.component_tmperson.entity.WalletDebit;
import com.chainyoung.component_tmperson.entity.WalletRepayment;
import com.chainyoung.component_tmperson.ui.adapter.WalletRepaymentScheduleAdapter;
import com.chainyoung.component_tmperson.ui.mvp.contract.WalletRepaymentScheduleContract;
import com.chainyoung.component_tmperson.ui.mvp.presenter.WalletRepaymentSchedulePresenter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WalletRepaymentScheduleActivity extends BaseActivity {

    @BindView(R2.id.txt_rate)
    TextView txtRate;
    @BindView(R2.id.tv_total_repay_amount)
    TextView tvTotalRepayAmount;
    @BindView(R2.id.rv_list)
    RecyclerView rvList;

    private WalletDebit walletDebit;
    private WalletDebit selectPeriod;
    private String debitAmount;
    private String periodId;
    private List<WalletRepayment> mData = new ArrayList<>();
    private WalletRepaymentScheduleAdapter adapter;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected void initUI() {
        setCurrentTitle("还款方式");
        walletDebit = (WalletDebit) getIntent().getSerializableExtra("walletDebit");
        selectPeriod = (WalletDebit) getIntent().getSerializableExtra("selectPeriod");
        mData = (List<WalletRepayment>) getIntent().getSerializableExtra("repaymentList");
        txtRate.setText(String.format("每月还款额度一样，日息%s%%", StringUtil.subZeroAndDot(String.valueOf(selectPeriod.getRate()))));
        init();
        initRecylerview();
    }


    private void init() {

    }

    private void initRecylerview() {
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new WalletRepaymentScheduleAdapter(mData, walletDebit.getCoin());
        rvList.setAdapter(adapter);
        DividerItemDecoration itemDecoration = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        itemDecoration.setDrawable(ContextCompat.getDrawable(mContext, R.drawable.shape_divider_gray));
        rvList.addItemDecoration(itemDecoration);
    }

    @Override
    protected void initData() {
        if (!mData.isEmpty()) {
            BigDecimal totalAmount = BigDecimal.ZERO;
            for (WalletRepayment repayment : mData) {
                totalAmount = totalAmount.add(repayment.getInterests()).add(repayment.getPrincipal());
            }
            tvTotalRepayAmount.setText("总计应还" + StringUtil.subZeroAndDot(String.valueOf(totalAmount)) + walletDebit.getCoin().toUpperCase());
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_activity_wallet_repayment_schedule;
    }


}
